package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

import model.Job;
import model.Park;
import model.Role;
import model.User;
import controller.Controller;
/**
 * Shows a list of jobs and their attributes.
 * 
 * @author James Gunter - jpgunter
 *
 */
public class JobListView extends JPanel implements ActionListener {
  
  public static int PREFFERED_HEIGHT = 400;
  public static int PREFFERED_WIDTH = 800;
  
  private DateFormat my_date_format;
  private String[][] my_table_data;
  
  private MainWindow my_parent;
  private JButton my_back_button;
  private JButton my_home_button;
  private JTable my_jobs_table;
  private List<Job> my_jobs;
  private List<Job> my_current_jobs;
  private JComboBox<String> my_park_filter;
  private JComboBox<String> my_district_filter;
  private JComboBox<String> my_region_filter;
  private JButton my_filter_reset_button;
  
  /**
   * Create the panel.
   */
  public JobListView(MainWindow the_parent, Controller the_controller, final List<Job> the_jobs) {
    
    my_jobs = the_jobs;
    my_current_jobs = my_jobs;
    
    my_parent = the_parent;
    
    setPreferredSize(new Dimension(PREFFERED_WIDTH, PREFFERED_HEIGHT));
    
    my_date_format = new SimpleDateFormat("M/d/y");
    
    setLayout(new BorderLayout());
    
    JPanel header_panel = new JPanel(new BorderLayout());
    JLabel lblJob = new JLabel("Jobs (Click to View Details)");
    lblJob.setHorizontalAlignment(SwingConstants.CENTER);
    header_panel.add(lblJob, BorderLayout.CENTER);
    
    my_back_button = new JButton("Back");
    my_back_button.addActionListener(this);
    header_panel.add(my_back_button, BorderLayout.WEST);
    
    my_home_button = new JButton("Home");
    my_home_button.addActionListener(this);
    header_panel.add(my_home_button, BorderLayout.EAST);
    
    add(header_panel, BorderLayout.NORTH);
    
    JPanel inner_panel = new JPanel(new BorderLayout());
    
    JPanel filters_panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
    //populate park filter
    my_park_filter = new JComboBox<String>();
    my_park_filter.addItem("<Pick Park>");
    for(String p : the_controller.getParkNames()){
      my_park_filter.addItem(p);
    }
    my_park_filter.addActionListener(this);
    filters_panel.add(my_park_filter);
    //populate district filter
    my_district_filter = new JComboBox<String>();
    my_district_filter.addItem("<Pick District>");
    for(String d : the_controller.getDistricts()){
      my_district_filter.addItem(d);
    }
    my_district_filter.addActionListener(this);
    filters_panel.add(my_district_filter);
    //populate region filter
    my_region_filter = new JComboBox<String>();
    my_region_filter.addItem("<Pick Region>");
    for(String r : the_controller.getRegions()){
      my_region_filter.addItem(r);
    }
    my_region_filter.addActionListener(this);
    filters_panel.add(my_region_filter);
    
    my_filter_reset_button = new JButton("Reset Filter");
    my_filter_reset_button.addActionListener(this);
    filters_panel.add(my_filter_reset_button);
    
    inner_panel.add(filters_panel, BorderLayout.NORTH);
    
    JScrollPane scrollPane = new JScrollPane();
    inner_panel.add(scrollPane, BorderLayout.CENTER);
    
    my_jobs_table = new JTable();
    populateTable(my_current_jobs);
    scrollPane.setViewportView(my_jobs_table);
    
    my_jobs_table.addMouseListener(new MouseAdapter() {

      @Override
      public void mouseClicked(MouseEvent the_event){
        //Change to get the job by name.
        my_parent.showJob(my_current_jobs.get(my_jobs_table.getSelectedRow()));
      }
    });
    
    add(inner_panel, BorderLayout.CENTER);
  }
  
  private void populateTable(List<Job> the_jobs) {
    String[] table_headers = new String[] {
        "Name", "Park", "Date", "Duration", "Light", "Medium", "Heavy"
      };
    my_table_data = new String[the_jobs.size()][7];
    
    for(int i=0;i<the_jobs.size();i++){
      my_table_data[i][0] = the_jobs.get(i).getTitle();
      my_table_data[i][1] = the_jobs.get(i).getPark().getName();
      my_table_data[i][2] = my_date_format.format(the_jobs.get(i).getDate());
      my_table_data[i][3] = the_jobs.get(i).getDuration() + "";
      my_table_data[i][4] = the_jobs.get(i).getLightWorkFilled() + "/" + the_jobs.get(i).getLightWorkNeeded();
      my_table_data[i][5] = the_jobs.get(i).getMediumWorkFilled() + "/" + the_jobs.get(i).getMediumWorkNeeded();
      my_table_data[i][6] = the_jobs.get(i).getHeavyWorkFilled() + "/" + the_jobs.get(i).getHeavyWorkNeeded();
    }
    
    my_jobs_table.setModel(new DefaultTableModel(my_table_data, table_headers));
    
  }
  /**
   * Listener for the buttons on the panel.
   */
  @Override
  public void actionPerformed(ActionEvent e) {
    if(e.getSource() == my_back_button){
      my_parent.back();
    } else if (e.getSource() == my_home_button){
      my_parent.home();
    } else if (e.getSource() == my_park_filter){
      String selected_park = (String)my_park_filter.getSelectedItem();
      if(!selected_park.startsWith("<")){
        filterJobsByPark(selected_park);
        my_district_filter.setSelectedIndex(0);
        my_region_filter.setSelectedIndex(0);
      }
    } else if (e.getSource() == my_district_filter){
      String selected_district = (String)my_district_filter.getSelectedItem();
      if(!selected_district.startsWith("<")){
        filterJobsByDistrict(selected_district);
        my_park_filter.setSelectedIndex(0);
        my_region_filter.setSelectedIndex(0);
      }
    } else if (e.getSource() == my_region_filter){
      String selected_region = (String)my_region_filter.getSelectedItem();
      if(!selected_region.startsWith("<")){
        filterJobsByRegion(selected_region);
        my_park_filter.setSelectedIndex(0);
        my_district_filter.setSelectedIndex(0);
      }
    } else if (e.getSource() == my_filter_reset_button){
      my_park_filter.setSelectedIndex(0);
      my_district_filter.setSelectedIndex(0);
      my_region_filter.setSelectedIndex(0);
      my_current_jobs = my_jobs;
      populateTable(my_current_jobs);
    }
  }
  
  private void filterJobsByDistrict(String the_district){
    
    my_current_jobs = new ArrayList<Job>();
    for(Job j : my_jobs){
      if(j.getPark().getDistrict().equalsIgnoreCase(the_district)){
        my_current_jobs.add(j);
      }
    }
    populateTable(my_current_jobs);
  }
  private void filterJobsByRegion(String the_region){
    my_current_jobs = new ArrayList<Job>();
    for(Job j : my_jobs){
      if(j.getPark().getRegion().equalsIgnoreCase(the_region)){
        my_current_jobs.add(j);
      }
    }
    populateTable(my_current_jobs);
  }
  private void filterJobsByPark(String the_park){
    my_current_jobs = new ArrayList<Job>();
    for(Job j : my_jobs){
      if(j.getPark().getName().equalsIgnoreCase(the_park)){
        my_current_jobs.add(j);
      }
    }
    populateTable(my_current_jobs);
  }
}
